package com.Pan;

import com.Pan.mapper.GskOtcMapper;
import com.Pan.pojo.GskOtc;
import com.Pan.pojo.SubmitGroup;
import com.Pan.pojo.SubmitImage;
import com.Pan.pojo.SubmitReq;
import com.Pan.util.BusinessThreadPool;
import com.Pan.util.HttpConnectionUtils;
import com.Pan.util.JsonUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Future;

/**
 * @author lipanJia
 * @date 2021-11-22 18:16
 */
@SpringBootTest
public class GskResponse {

    /**
     * 标准环境url提交：url = "https://mobile.lenztechretail.com/business/api/trax/imgRecog/urlSubmit";
     * <p>
     * 标准环境base64提交：url = "https://mobile.lenztechretail.com/business/api/trax/imgRecog/base64submit";
     */
    // 标准环境url提交：
    private final static String BZ_URL_SUBMIT = "https://mobile.lenztechretail.com/business/api/trax/imgRecog/urlSubmit";

    // 腾讯云环境url提交：
    private final static String TX_URL_SUBMIT = "http://mobileout.langjtech.com/business/api/trax/imgRecog/urlSubmit";

    // 标准环境base64提交
    private final static String BZ_BASE_64_SUBMIT = "https://mobile.lenztechretail.com/business/api/trax/imgRecog/base64submit";

    // UAT环境url提交
    private final static String UAT_URL_SUBMIT = "https://perfeitti-uat.langjtech.com/business/api/trax/imgRecog/urlSubmit";

    // UAT环境url提交
    private final static String UAT_URL_BASE64_SUBMIT = "http://ml-uat.langjtech.com/business/api/trax/imgRecog/base64submit";

    // 腾讯环境base64提交
    private final static String TX_BASE_64_SUBMIT = "http://mobileout.langjtech.com/business/api/trax/imgRecog/base64submit";

    // K8S URL提交
    private final static String NEW_TEST_SUBMIT = "http://81.70.127.94/business/api/trax/pec/imgRecog/urlSubmit";

    // K8S
    private final static String NEW_TEST_BASE64_SUBMIT = "http://81.70.127.94/business/api/trax/pec/imgRecog/base64submit";

    @Autowired
    private GskOtcMapper gskOtcMapper;

    /**
     * GSK翻拍项目问卷提交
     *
     * @throws JSONException
     * @throws InterruptedException
     */
    @Test
    public void contextNewGskOtc() {

        // 输入问卷的taskId
        String taskId = "20220510111501311823440644542464";
        // 输入问卷的Qid
        String qId = "20220510133012311857460539428864";
        try {
            List<GskOtc> responseIdNull = gskOtcMapper.getNewResponseIdNull(null);

            if (responseIdNull.size() == 0) {
                return;
            }
            List<List<GskOtc>> partition = Lists.partition(responseIdNull, 5);
            for (List<GskOtc> gskOtcs : partition) {
                List<Future> futureList = Lists.newArrayList();
                for (GskOtc gskOtc : gskOtcs) {
                    futureList.add(BusinessThreadPool.getInstance().execute(() -> requestToApi(gskOtc, taskId, qId)));
                }
                for (Future future : futureList) {
                    try {
                        future.get();
                    } catch (Exception e) {
                        throw new Exception();
                    }
                }
                Thread.sleep(5000);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        contextNewGskOtc();
    }

    /**
     * GSK翻拍项目问卷提交
     *
     * @throws JSONException
     * @throws InterruptedException
     */
    private String requestToApi(GskOtc gskOtc, String taskId, String qID) {
        // 拿batchId相同的几条数据放在一个list数组中，依据batchId
        // 组装请求json参数，传过去每一个batchId，和batchId对应的list数组，接收处理好的json信息，传输包括taskid，qid
        String json = getGskOtcJson(gskOtc, taskId, qID);
        System.out.println(json);
        // 调用远程http请求
        String retJson = HttpConnectionUtils.post(GskResponse.UAT_URL_SUBMIT, json);
        System.out.println(retJson);

        // 解析响应报文
        JSONObject jsonObject = JSONObject.parseObject(retJson);
        // 将新生成的rid，批量更新到
        String NewResponseId = (String) jsonObject.getJSONObject("retData").get("responseId");
        String url = gskOtc.getUrl();
        Integer id = gskOtc.getId();
        gskOtcMapper.updateNewResponseUrl(NewResponseId, url, id, url);

        if (NewResponseId.isEmpty()) {
            System.out.println("提交失败" + gskOtc);
        } else {
            System.out.println(gskOtc + "已提交");
        }
        return "success";
    }

    private String getGskOtcJson(GskOtc testTempList, String taskId, String qId) {
        // 接收batchId，和关于本条batchId相同的几份答卷。
        // 首先处理每一个答卷最里层图片集合
        // 创建一个最里层图片list集合对象
        List<SubmitImage> imageList = new ArrayList<>();
        int i = 1;
        // 创建一个图片集合对象
        SubmitImage submitImage = new SubmitImage();
        // 取出url，set进去，set每一个参数的值
        submitImage.setUrl(testTempList.getUrl());
        submitImage.setId(String.valueOf(i++));
        submitImage.setColumnNo(i);
        submitImage.setRowNo(1);
        // 把set进去的值存入list数组，
        imageList.add(submitImage);


        // 处理第二层图片组，创建图片组list集合
        List<SubmitGroup> pictureGroupList = new ArrayList<>();
        // 创建图片组对象，set数据
        SubmitGroup group = new SubmitGroup();
        group.setGroupId("01");
        group.setqId(qId);
        // 这里传入上一步保存好的list图片集合
        group.setGroupPictureList(imageList);
        // 把set进去的值存入创建的图片组list集合
        pictureGroupList.add(group);

        // 处理最外层请求对象,创建最外层对象，set值
        SubmitReq req = new SubmitReq();
        req.setCompanyId("GskFP");
        req.setBusinessDataParamList(Arrays.asList("1", UUID.randomUUID().toString()));
        req.setTaskId(taskId);
        // 这里传入上一步保存好的图片组list集合
        req.setPictureGroupList(pictureGroupList);
        // 返回拼接好的json信息
        return JsonUtil.toJsonString(req);
    }
}
